package com.topnav.mc.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.js.lib.annotation.ApiLogger;
import com.js.lib.result.Result;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.entity.DeviceChannel;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceChannelBean;
import com.topnav.mc.admin.entity.param.DeviceChannelParam;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.entity.wrapper.DeviceChannelWrapper;
import com.topnav.mc.admin.service.IDeviceChannelService;
import com.topnav.mc.admin.service.IDeviceService;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.involve.http.HttpUtil;
import com.topnav.mc.uitls.GbServerUtil;
import com.topnav.mc.uitls.ResultErrorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "admin/channel", tags = "设备视频通道管理")
@RestController
@CrossOrigin
@RequestMapping("admin/channel")
@Slf4j
public class DeviceChannelController extends McController<DeviceChannel, DeviceChannelBean, DeviceChannelParam, IDeviceChannelService, DeviceChannelWrapper> {
    @Autowired
    private IDeviceChannelService deviceChannelService;
    @Autowired
    private IDeviceService deviceService;


    @ApiOperation("同步通道状态")
    @PostMapping("/{deviceId}/sync")
    public Result get(@PathVariable String deviceId){
        QueryWrapper<DeviceParam> wrapper = new QueryWrapper<>();
        wrapper.eq("d.code",deviceId);
        wrapper.or().eq("d.id",deviceId);
        DeviceBean deviceBean = deviceService.getOneInfo(wrapper,false);

        String url = GbServerUtil.get();
        if (url == null){
            url = "http://" + deviceBean.getGbServerHost() + ":" + deviceBean.getGbServerApiPort();
        }
        url = url +  "/api/device/query/devices/"+deviceBean.getCode()+"/sync";

        try{
            HttpUtil.doPost(url,null,null,30000);
        }catch (Exception e){
            log.error(">>>>>>: {}",e.toString());
            return ResultErrorUtil.byExceptionMsg (ExceptionMsg.EXCEPTION,e.toString());
        }
        return Result.ok();
    }
}
